from django.db import models


class Assets(models.Model):
    """资产表"""
    asset_type_choice = (
        ('server', '服务器'),
        ('networkdevice', '网络设备'),
        ('storagedevice', '存储设备'),
        ('securitydevice', '安全设备'),
        ('other', '其他'),
    )

    asset_status_choice = (
        (0, '在线'),
        (1, '离线'),
        (2, '故障'),
        (3, '备用'),
        (4, '其他'),
    )

    asset_type = models.CharField(choices=asset_type_choice, max_length=64, default='server', verbose_name="资产类型")
    asset_status = models.SmallIntegerField(choices=asset_status_choice, default=0, verbose_name='设备状态')
    asset_name = models.CharField(max_length=64, unique=True, verbose_name='资产名称')
    mac = models.CharField(max_length=128, unique=True, verbose_name='MAC地址')
    tags = models.ManyToManyField('Tag', blank=True, verbose_name='标签')
    idc = models.ForeignKey('IDC', on_delete=models.CASCADE, null=True, blank=True, verbose_name='IDC')
    added_time = models.DateTimeField(auto_now_add=True, verbose_name='上架时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    dismounted_time = models.DateTimeField(verbose_name='下架时间')
    remarks = models.TextField(null=True, blank=True, verbose_name='备注')

    def __str__(self):
        return '<%s>  %s' % (self.get_asset_type_display(), self.asset_name)

    class Meta:
        verbose_name = '资产表'
        verbose_name_plural = '资产总表'
        ordering = ['-added_time']


class Server(models.Model):
    """服务器设备"""
    server_type_choice = (
        (0, 'PC设备'),
        (1, '刀片机'),
        (2, '小型机'),
        (3, '虚拟设备'),
    )

    os_type_choice = (
        (0, 'Linux系统'),
        (1, 'Windows系统'),
        (2, 'MAC系统'),
    )

    asset = models.OneToOneField('Assets', on_delete=models.CASCADE)
    server_type = models.SmallIntegerField(choices=server_type_choice, default=0, verbose_name='服务器类型')
    hosted_on = models.ForeignKey('self', on_delete=models.CASCADE, related_name='hosted_on_server',
                                  blank=True, null=True, verbose_name='宿主机')   # 虚拟机专用字段
    server_ip = models.GenericIPAddressField(verbose_name='服务器IP')
    os_type = models.SmallIntegerField(choices=os_type_choice, default=0, verbose_name='操作系统类型')
    ram = models.CharField(max_length=128, verbose_name='内存')
    disk = models.CharField(max_length=128, verbose_name='硬盘')
    CPU = models.CharField(max_length=128, verbose_name='CPU')

    class Meta:
        verbose_name = '服务器'
        verbose_name_plural = '服务器'


class NetworkDevice(models.Model):
    """网络设备"""
    ND_type_choice = (
        (0, '路由器'),
        (1, '交换机'),
        (2, '无线AP'),
    )

    asset = models.OneToOneField('Assets', on_delete=models.CASCADE)
    ND_type = models.SmallIntegerField(choices=ND_type_choice, default=0, verbose_name="网络设备类型")
    vlan_ip = models.GenericIPAddressField(blank=True, null=True, verbose_name="VLan_IP")
    manage_ip = models.GenericIPAddressField(blank=True, null=True, verbose_name="管理IP")
    model = models.CharField(max_length=128, null=True, blank=True, verbose_name="网络设备型号")
    firmware = models.CharField(max_length=128, blank=True, null=True, verbose_name="设备固件版本")
    port_num = models.SmallIntegerField(null=True, blank=True, verbose_name="端口个数")
    device_config = models.TextField(null=True, blank=True, verbose_name="设备配置")

    def __str__(self):
        return '%s--%s--%s <mac:%s>' % (self.asset.asset_name, self.get_ND_type_display(), self.model, self.asset.mac)

    class Meta:
        verbose_name = '网络设备'
        verbose_name_plural = "网络设备"


class IDC(models.Model):
    """机房"""
    name = models.CharField(max_length=64, unique=True, verbose_name="机房名称")
    memo = models.CharField(max_length=128, blank=True, null=True, verbose_name="备注")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '机房'
        verbose_name_plural = '机房'


class Tag(models.Model):
    """标签"""
    name = models.CharField('标签名', max_length=32, unique=True)
    c_day = models.DateField('创建日期', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = '标签'

